package com.lanyou.esb.servicemix.util;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.core.MediaType;

import org.apache.cxf.jaxrs.client.WebClient;

import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
import com.lanyou.cook.common.Constants;

public class BundleHandleUtil {

	
	/**
	 * 获取所有的bundle信息
	 * @return
	 */
	public static String getBundles(String bundleRequestAddress){
		List<Object> providerList = new ArrayList<Object>();
        providerList.add(new JacksonJsonProvider());
        
        String productList = WebClient.create(bundleRequestAddress, providerList)
            	.path("/" + Constants.BUNDLES)
            	.accept(MediaType.APPLICATION_JSON)
                .get(String.class);
        System.out.println(productList);
        
		return productList;
	}
	
	
	
	/**
	 * 安装bundle API
	 * @param location
	 * @param bundlePath
	 * @param isStart
	 * @return
	 */
	public static String installBundle(String location, String bundlePath, String isStart, String bundleRequestAddress){
		List<Object> providerList = new ArrayList<Object>();
        providerList.add(new JacksonJsonProvider());
        
        String productList = WebClient.create(bundleRequestAddress, providerList)
            	.path("/" + Constants.INSTALL_BUNDLE).query("location", location).query("bundlePath", bundlePath).query("isStart", isStart)
            	.accept(MediaType.APPLICATION_JSON)
                .get(String.class);
        System.out.println(productList);
        
		return productList;
	}
	
	/**
	 * 更新bundle API
	 * @param id
	 * @param bundlePath
	 * @return
	 */
	public static String updateBundle(String id, String bundlePath, String bundleRequestAddress){
		
		List<Object> providerList = new ArrayList<Object>();
        providerList.add(new JacksonJsonProvider());
        
        String productList = WebClient.create(bundleRequestAddress, providerList)
        		.path("/" + Constants.UPDATE_BUNDLE).query("id", id).query("bundlePath", bundlePath)
            	.accept(MediaType.APPLICATION_JSON)
                .get(String.class);
        System.out.println(productList);
        
		return productList;
	}
	
	/**
	 * 操作bundle API(启动、停止、卸载和刷新)
	 * @param id
	 * @param action
	 * @return
	 */
	public static String excuteBundle(String id, String action, String bundleRequestAddress){
		List<Object> providerList = new ArrayList<Object>();
        providerList.add(new JacksonJsonProvider());
        
        String productList = WebClient.create(bundleRequestAddress, providerList)
        		.path("/" + Constants.EXECUTE_BUNDLE).query("id", id).query("action", action)
            	.accept(MediaType.APPLICATION_JSON)
                .get(String.class);
        System.out.println(productList);
        
		return productList;
	}
	
/*	public static void main(String[] args) throws FileNotFoundException {
			
	        String baseAddress = "http://localhost:8181/api";
	        List<Object> providerList = new ArrayList<Object>();
	        providerList.add(new JacksonJsonProvider());
	
	        List productList = WebClient.create(baseAddress, providerList)
	            .path("/getProducts/")
	            .accept(MediaType.APPLICATION_JSON)
	            .get(List.class);
	        
	        
	        
	        for (Object product : productList) {
	            System.out.println(product);
	        }
			
			String location = "gson-2.7.jar";
			String bundlePath = "/usr/uploadbundles/gson-2.7.jar";
			String isStart = "1";
			String baseAddress = "http://172.26.153.196:8080/cxf/monitor/services";
			
			
	        List<Object> providerList = new ArrayList<Object>();
	        providerList.add(new JacksonJsonProvider());
	
	        List productList = WebClient.create(baseAddress, providerList)
	            .path("/bundles")
	            .accept(MediaType.APPLICATION_JSON)
	            .get(List.class);
	        
	        for (Object product : productList) {
	        	System.out.println(product);
	        }
	
	        
	       String productList = WebClient.create(baseAddress, providerList)
	        	.path("/installBundle").query("location", location).query("bundlePath", bundlePath).query("isStart", isStart)
	        	.accept(MediaType.APPLICATION_JSON)
	            .get(String.class);
	        System.out.println(productList);
	        
	        
	        
	        String productList = WebClient.create(baseAddress, providerList)
	            .path("/updateBundle").query("id", id).query("bundlePath", bundlePath)
	            .accept(MediaType.APPLICATION_JSON)
	            .get(String.class);
	        System.out.println(productList);
	        
	        Gson gson = new Gson();
           
	}*/
 
}
